// Event
DLSS.Event = Ext.data.Record.create([
    {name: 'id'},
    {name: 'title', type: 'string'},
    {name: 'date', type: 'string'},
    {name: 'type', type: 'string'},
    {name: 'rank', type: 'string'},
    {name: 'place', type: 'string'},
    {name: 'organizer', type: 'string'},
    {name: 'masterOfCeremony', type: 'string'},
    {name: 'chairman', type: 'string'},
    {name: 'music', type: 'string'},
    {name: 'scrutineer', type: 'string'},
    {name: 'support', type: 'string'}
]);

DLSS.eventsReader = new Ext.data.XmlReader({record:'event'},
	['id', 'title', 'date', 'type', 'rank', 'place', 'organizer',
	'masterOfCeremony', 'chairman', 'music', 'scrutineer', 'support'], DLSS.Event);

DLSS.eventsStore = new Ext.data.Store({
    url: 'eventsList.htm',
    reader: DLSS.eventsReader,
    sortInfo:{field:'date', direction:'ASC'}
});

DLSS.eventsColumnModel = new Ext.grid.ColumnModel([
    {id:'event', header: "Title", width: 50, sortable: true, dataIndex: 'title', editor: new Ext.form.TextField({allowBlank: false})},
    {header: "Date", width: 40, sortable: true, dataIndex: 'date', editor: new Ext.form.TextField({allowBlank: false})},
    {header: "Type", width: 20, sortable: true, dataIndex: 'type', editor: new Ext.form.TextField({allowBlank: false})},
    {header: "Rank", width: 20, sortable: true, dataIndex: 'rank', editor: new Ext.form.TextField({allowBlank: false})},
    {header: "Place", width: 20, sortable: true, dataIndex: 'place', editor: new Ext.form.TextField({allowBlank: false})},
    {header: "Organizer", width: 20, sortable: true, dataIndex: 'organizer', editor: new Ext.form.TextField({allowBlank: false})},
    {header: "Master Of Ceremony", width: 20, sortable: true, dataIndex: 'masterOfCeremony', editor: new Ext.form.TextField({allowBlank: false})},
    {header: "Chairman", width: 20, sortable: true, dataIndex: 'chairman', editor: new Ext.form.TextField({allowBlank: false})},
    {header: "Music", width: 20, sortable: true, dataIndex: 'music', editor: new Ext.form.TextField({allowBlank: false})},
    {header: "Scrutineer", width: 20, sortable: true, dataIndex: 'scrutineer', editor: new Ext.form.TextField({allowBlank: false})},
    {header: "Support", width: 20, sortable: true, dataIndex: 'support', editor: new Ext.form.TextField({allowBlank: false})}
]);
DLSS.eventsColumnModel.defaultSortable = true;

// Event CRUD form
DLSS.eventsForm = new Ext.FormPanel({
	frame:true,
    border:false,
    defaults:{autoHeight: true},
    items:[
       	new Ext.form.Hidden({id:'fId', name:'id'}),
       	new Ext.form.TextField({id:'fTitle', fieldLabel:'Title', name:'title'}),
       	new Ext.form.TextField({id:'fDate', fieldLabel:'Date', name:'date'}),
  	   	new Ext.form.TextField({id:'fType', fieldLabel:'Type', name:'type'}),
  	   	new Ext.form.TextField({id:'fRank', fieldLabel:'Rank', name:'rank'}),
  	   	new Ext.form.TextField({id:'fPlace', fieldLabel:'Place', name:'place'}),
  	   	new Ext.form.TextField({id:'fOrganizer', fieldLabel:'Organizer', name:'organizer'}),
  	   	new Ext.form.TextField({id:'fMasterOfCeremony', fieldLabel:'Master of ceremony', name:'masterOfCeremony'}),
  	   	new Ext.form.TextField({id:'fChairman', fieldLabel:'Chairman', name:'chairman'}),
  	   	new Ext.form.TextField({id:'fMusic', fieldLabel:'Music', name:'music'}),
  	   	new Ext.form.TextField({id:'fScrutineer', fieldLabel:'Scrutineer', name:'scrutineer'}),
  	   	new Ext.form.TextField({id:'fSupport', fieldLabel:'Support', name:'support'}),
  	]
});

// Modal Event window
DLSS.eventsModal = {
	
	add: function() {
		DLSS.eventsForm.getComponent('fId').setValue('');
		DLSS.eventsForm.getComponent('fTitle').setValue('');
		DLSS.eventsForm.getComponent('fDate').setValue('');
		DLSS.eventsForm.getComponent('fType').setValue('');
		DLSS.eventsForm.getComponent('fRank').setValue('');
		DLSS.eventsForm.getComponent('fPlace').setValue('');
		DLSS.eventsForm.getComponent('fOrganizer').setValue('');
		DLSS.eventsForm.getComponent('fMasterOfCeremony').setValue('');
		DLSS.eventsForm.getComponent('fChairman').setValue('');
		DLSS.eventsForm.getComponent('fMusic').setValue('');
		DLSS.eventsForm.getComponent('fScrutineer').setValue('');
		DLSS.eventsForm.getComponent('fSupport').setValue('');
		DLSS.eventsModal.show(DLSS.getMessage('events_add'), DLSS.eventsForm);
	},
	
	edit: function(record/*sm*/) {
        //var record = sm.getSelected();
        if(record) {
			DLSS.eventsForm.getComponent('fId').setValue(record.get('id'));
			DLSS.eventsForm.getComponent('fTitle').setValue(record.get('title'));
			DLSS.eventsForm.getComponent('fDate').setValue(record.get('date'));
			DLSS.eventsForm.getComponent('fType').setValue(record.get('type'));
			DLSS.eventsForm.getComponent('fRank').setValue(record.get('rank'));
			DLSS.eventsForm.getComponent('fPlace').setValue(record.get('place'));
			DLSS.eventsForm.getComponent('fOrganizer').setValue(record.get('organizer'));
			DLSS.eventsForm.getComponent('fMasterOfCeremony').setValue(record.get('masterOfCeremony'));
			DLSS.eventsForm.getComponent('fChairman').setValue(record.get('chairman'));
			DLSS.eventsForm.getComponent('fMusic').setValue(record.get('music'));
			DLSS.eventsForm.getComponent('fScrutineer').setValue(record.get('scrutineer'));
			DLSS.eventsForm.getComponent('fSupport').setValue(record.get('support'));
		}
	    DLSS.eventsModal.show(DLSS.getMessage('events_edit'), DLSS.eventsForm);
	},
	
	show: function(title, items) {
		var win;
		// create the window on the first click and reuse on subsequent clicks
		if(!win){
		     win = new Ext.Window({
		         title: title,
		         layout:'fit',
		         modal:true,
		         border:false,
		         width:300,
		         height:350,
		         closeAction:'hide',
		         plain: true,
		
		         items: items,
		
		         buttons: [
		         	{text:DLSS.getMessage('button_submit'), handler: function(){DLSS.eventsModal.performAction(); win.hide();}},
		         	{text:DLSS.getMessage('button_close'), handler: function(){win.hide();}}
		         ]
		     });
		 }
		 win.show(this);    
	},
	
	performAction: function() {
  		DLSS.eventsForm.form.submit({url: 'eventUpdate.htm', waitMsg: 'Saving Data...', success: DLSS.eventsModal.doneFunction});
    }, 
  
   doneFunction: function(form,action) {
    	//alert("success");
    	DLSS.eventsStore.reload();
   }
	
};